package com.jivam.lottery.panel;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.jivam.lottery.data.DataCalculator;
import com.jivam.lottery.data.DataUtil;
import com.jivam.lottery.model.PeriodNum;
import com.jivam.lottery.util.Constants;
import com.jivam.lottery.util.ShowUtil;

/**
 * 历史期数面板
 * @author Jivam
 */
public class PeriodPanel extends AbstractPanel{

	private static final long serialVersionUID = 5007652692581410007L;

	private JPanel center;

	public PeriodPanel() {
		init();
	}


	@Override
	protected void init() {
		this.setLayout(new BorderLayout());
		
		JPanel north = new JPanel();
		north.setLayout(new GridLayout(1,9));
		for(String title : Constants.TITLE_PERIODPANEL){
			north.add(new JLabel(title,SwingConstants.CENTER));
		}
		
		center = new JPanel();
		center.setLayout(new GridLayout(Constants.MAX_SHOW_PERIOD, Constants.TITLE_PERIODPANEL.length));
		reloadPanel();
		this.add(north,BorderLayout.NORTH);
		this.add(center,BorderLayout.CENTER);
	}

	

	@Override
	public void reloadPanel() {
		center.removeAll();
		LinkedList<List<JLabel>> pls = new LinkedList<List<JLabel>>();
		try {
			Iterator<PeriodNum> it = DataCalculator.pns.descendingIterator();
			int i = 0;
			while (it.hasNext() && i < Constants.MAX_SHOW_PERIOD) {
				pls.add(ShowUtil.getLabelListByPeriodNum(it.next()));
				i++;
			}
			Collections.reverse(pls);
			for(List<JLabel> pl : pls){
				for(JLabel l : pl){
					center.add(l);
				}
			}
		} catch (Exception e) {
			center.add(new JLabel("PeriodPanel 初始化错误"+e.getMessage()));
			DataUtil.writeLog(e);
		}
	}

}
